<script type="text/javascript">
	$(document).ready(function(){
		var data = "<?php if(isset($_GET['data'])){ echo $_GET['data']; } ?>";
		
		var arr = data.split("*");
		// $.each(arr, function(key, val){
			// console.log(key + ":" + val);
		// });
		var tempcountry = '';
		var afids= '';
		console.log(arr);
		
		if( arr[0] === "COUNTRY" ){
			var result = data.split("|");
			$("#query").text(arr[2]);

			console.log(result);			
			console.log(result[0]);			
	
			$.each(result, function(key, val){
				if(key !=0 && val) {
					console.log(val);
					var temp = val.split("*");
					console.log("---"+temp);
					$(".result-body").append("<h3>"+ temp[2] +"</h3>");
					
					
					$.post("query/query-data.php", {search: arr[2], flag: "afcountry"}, function(result){

						var txt = '';
						$.each(result, function(key, val){
							txt += val;
						});
						
						txt = txt.replace(/[`~!@#$%^&()_+\=?;'<>\{\}\[\]\\\/]/gi, '');
						txt = txt.replace(/","/gi, '*');
						txt = txt.replace(/"/g, '');
						//console.log(txt);
						var temp2 = txt.split("|");
						
						$.each( temp2, function( a, b ){
							if( b ){
								if( a != 0 ){
									b = b.replace("*", "");
								}
								var af = b.split('*');
								
								$(".result-body").append(
									"<div style='position:relative; top:2px; left:10px; width: 800px;'>" +
										"<input type='radio' name='choices' style='margin-left:10px; margin-bottom:5px;' value='0|"+ af[8] +"'> "+ af[9] +" Rates</input></br></br>" +
									"</div>"
								);
							}
						});
					});
					
					$(".result-footer").append(
						"<input type='submit' id='btnNext' class='form-viewrates'/>"
					);
				}
			});
	
		}else if( arr[0] == "CITY" ){
			var result = data.split("|");
			$("#query").text(arr[2]);
			tempcountry = arr[2];
			console.log(result);
			
			$.post("query/query-data.php", {search: arr[3], flag: "country"}, function(result){
				
				var txt = '';
				$.each(result, function(key, val){
					txt += val;
				});

				txt = txt.replace(/[`~!@#$%^&()_+\=?;'"<>\{\}\[\]\\\/]/gi, '');
				
				var country = txt.split(",");
				
				$(".result-header").append(
					"<h2>" + arr[2]+ ", " + arr[4] + "</h2>" +
					"<p style='margin-top: -10px;'>" + country[0] + ", " + country[1] + "</p>" +
					"<hr>"
				);
			});
			
			$(".result-body").append(
					"<h3>Affiliates</h3><hr width='25%' size='1px' style='margin-top:-10px;'><br>"+
					"<input type='button' id='btnEditPrio' class='form-edit'/></br></br></br></br>"
			);
			
			$.each(result, function(key, val){
				if( key !=0 && val ){
					console.log(key + ":" + val);
					processCityData( val );
				}
			});
			
			$(".result-body").append(
				"<div style='position:absolute; top:60px; left:800px; width: 300px;'>" +
					"<h3>RALimo Rates</h3><hr size='1px' style='margin-top:-10px;'><br>" +
					"<input type='radio' name='choices' style='margin-left:10px; margin-bottom:5px;' value='1|"+ arr[2] +"'>"+ arr[2] +" Rates</input></br></br>" +
				"</div>"
			);
			
			$(".result-footer").append(
				"<input type='submit' id='btnNext' class='form-viewrates'/>"
			);

		}else if( arr[0] == "AFFILIATE" ){
			window.location.replace("view_affiliate.php?id="+arr[11]);
		
		}else if( arr[0] == "DRIVER" ){
			console.log("driver");
		
		}else if( arr[0] == "VEHICLE" ){
			var result = data.split("|");
			console.log(result);
			$("#query").text(arr[3]);
			
			$(".result-body").append(
				"<h3 style='display:inline;'>Vehicle type : </h3>"+ arr[1]+ "</br>"+
				"<h3 style='display:inline;'>Capacity     : </h3>"+ arr[2]+ "</br></br></br></br></br>"+
				"<h3>Affiliates with "+ arr[3] +":</h3><hr width='650px' size='1px' style='margin-top:-10px;'></br>"
			);
				
			$.post("query/query-data.php", {search: arr[3], flag:"afvehicle"}, function(result){
				
				var txt = '';
				$.each(result, function(key, val){
					txt += val;
				});
				
				txt = txt.replace(/[`~!@#$%^&()_+\=?;'<>\{\}\[\]\\\/]/gi, '');
				txt = txt.replace(/","/gi, '*');
				txt = txt.replace(/"/g, '');
				var temp2 = txt.split("|");
				
				$.each( temp2, function( a, b ){
					if( b ){
						if( a != 0 ){
							b = b.replace("*", "");
						}
						
						var newarr=b.split('*');
						
						$(".result-body").append(
							"<div style='position:relative; left:10px; top:10px; width: 700px;'>"+
								"<input type='radio' name='choices' value='3|"+ newarr[1] +"'>&nbsp;&nbsp;"+ newarr[2]+ "</input></br></br>"+
							"</div>"
						);
					}
				});
			});
			
			$(".result-footer").append(
				"<input type='submit' id='btnNext' class='form-viewmorev'/>"
			);
		}
		
		$("#btnNext").click(function(e){
			e.preventDefault();
			
			var choice = $("input[name=choices]:checked").val().split("|");
			console.log(choice);
			if( arr[0] == "CITY" ){
				if( choice[0] == "0" ){
					window.location.replace("view_affiliate.php?id="+choice[1]);
				}else{
					window.location.replace("view_ralimorates.php?city="+choice[1]);
				}
			}else if( arr[0] == "COUNTRY" ){
				if( choice[0] == "0" ){
					window.location.replace("view_affiliate.php?id="+choice[1]);
				}else{
					console.log("ralimo");
				}
			}else if( arr[0] == "VEHICLE" ){
				if( choice[0] == "3"){
					window.location.replace("affiliate_vehicles.php?id="+choice[1]);
				}else{
					console.log("vehicle");
				}
			}else if( arr[0] == "AFFILIATE" ){
			
			}else if( arr[0] == "DRIVER" ){
				console.log("driver");
			}
		});
		
		function processCityData( data ){
			var result = data.split("*");
			
			$(".result-body").append(
				"<div style='position:relative; top:2px; left:10px; width: 700px;'>" +
					"<tr><td><input type='radio' name='choices' style='margin-left:10px; margin-bottom:5px;' value='0|"+ result[1] +"'/>&nbsp;&nbsp;&nbsp;</td><td>"+ result[2] +"</td></tr></br></br>"+
				"</div>"
			);
		}
		
		// $('input:radio[name=choices]').change(function() {
        // var val = $('input:radio[name=choices]:checked');
			// alert(">>>"+val.val());
				// $('#prioritynum'+ctr).attr('disabled', false);
			//setNotificationStatus(val);
		// });
	
		
		// $("#btnEditPrio").click(function(e){
			// e.preventDefault();
			
			// var choice = $("input[name=choices]:checked").val().split("|");
			// if()
			// $('input[name="prioritynum[]"]').each(function(){
			  // alert($(this).val());
			// });
		// });;
	});
	
</script>
<!--  start nav-outer-repeat................................................... END -->

 <div class="clear"></div>
 
<!-- start content-outer ........................................................................................................................START -->
<div id="content-outer">
<!-- start content -->
<div id="content">

	<!--  start page-heading -->
	<div id="page-heading">
		<h1>Search result for: <i><span id="query"></span></i></h1>
	</div>
	<!-- end page-heading -->
	<div id="affiliate_search_menu">
		<a href="panel.php?page=add_affiliate"><img style="border:none;" alt="add affiliate" src="images/forms/add_affiliate.gif" /></a>
	</div>
	
	<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
	<tr>
		<th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
		<th class="topleft"></th>
		<td id="tbl-border-top">&nbsp;</td>
		<th class="topright"></th>
		<th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
	</tr>
	<tr>
		<td id="tbl-border-left"></td>
		<td>
		<div id="content-table-inner">
		
				<div id="result" style="min-height: 250px; position: relative;">
				<div class='result-header'></div>
				</br>
				
				<div class='result-body' style="padding: 10px; font-size: 14px; width: 600px;"></div>
				<div class='result-footer' style="position: absolute; bottom: 0px; right: 100px;"></div>
				
				<!-- <div class='result-footer' style="position: absolute; bottom: 0px; right: 100px;">
					<input type="submit" id="btnNext" class="form-next"/>
				</div> -->
			</div>
			
			<div class="clear"></div>
		 
		</div>
		</td>
		<td id="tbl-border-right"></td>
	</tr>
	<tr>
		<th class="sized bottomleft"></th>
		<td id="tbl-border-bottom">&nbsp;</td>
		<th class="sized bottomright"></th>
	</tr>
	</table>
	<div class="clear">&nbsp;</div>

</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<!--  end content-outer........................................................END -->

<div class="clear">&nbsp;</div>
